﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Threading;
using SSATool.Common.Logging;
using SSATool.Common.Util.Properties;

#endregion

namespace SSATool.Common.Util.Threading
{
    /// <summary>
    /// Represents a synchrnous process. This may be used for testing and
    /// mocking purposes.
    /// </summary>
    public abstract class ProcessBase : IProcess
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// This event will be fired if an exception occured during the
        /// thread execution.
        /// </summary>
        /// <remarks>
        /// This event will be fired in a separate thread execution context.
        /// </remarks>
        public event EventHandler<ThreadExceptionEventArgs> ThreadException;

        /// <summary>
        /// This event will be fired after the process execution has been
        /// finished.
        /// </summary>
        /// <remarks>
        /// This event will be fired in a separate thread execution context.
        /// </remarks>
        public event EventHandler<ResultEventArgs> Finished;

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="ProcessBase"/> class.
        /// </summary>
        protected ProcessBase()
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------


        /// <summary>
        /// Executes the action to run with the given data.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        /// <param name="data">Data to pass to the delegate to execute.</param>
        public abstract void Run<T>(Action<T> toRun, T data);

        /// <summary>
        /// Executes the action to run with the given data.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        /// <param name="data">Data to pass to the delegate to execute.</param>
        public abstract void Run<TArg, TRes>(Func<TArg, TRes> toRun, TArg data);

        /// <summary>
        /// Executes the action to run without an argument.
        /// </summary>
        /// <param name="toRun">A delegate to execute which must not be null.</param>
        public abstract void Run(Action toRun);

        /// <summary>
        /// Starts the execution of the specified functor.
        /// </summary>
        /// <param name="toRun">The functor object which should be executed.</param>
        protected void StartObservedExecution(Func<object> toRun)
        {
            object data = null;
            Exception error = null;

            try
            {
                data = toRun();
            }
            catch (Exception e)
            {
                Logger<DisposeBase>.Error(Resources.ExcAsyncProcess, e);
                OnThreadException(new ThreadExceptionEventArgs(e));
                error = e;
            }
            finally
            {
                OnFinished(new ResultEventArgs(data, error));                
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        protected virtual void OnThreadException(ThreadExceptionEventArgs e)
        {
            if (ThreadException != null)
            {
                ThreadException(this, e);
            }
        }

        protected virtual void OnFinished(ResultEventArgs e)
        {
            if (Finished != null)
            {
                Finished(this, e);
            }
        }

        #endregion
    }
}
